package dwei.log
{
	import mx.logging.ILogger;
	import mx.logging.LogEvent;
	import mx.logging.LogEventLevel;
	import mx.logging.targets.LineFormattedTarget;
	
	import dwei.core.util.StringUtil;
	
	public class LogTarget extends LineFormattedTarget
	{
//============================================================================
//PROPERTY
//============================================================================
		private var _ui:ILogUI;
//============================================================================
//CONSTRUCTOR
//============================================================================
		public function LogTarget(ui:ILogUI = null)
		{
			super();
			_ui = ui;
		}
//============================================================================
//OVERRIDE
//============================================================================
		override public function logEvent(event:LogEvent):void
		{
			var date:String = "";
			if (includeDate || includeTime)
			{
				var d:Date = new Date();
				if (includeDate)
				{
					date = StringUtil.fomatInt(d.getFullYear(), 4) + "." +
						StringUtil.fomatInt(d.getMonth() + 1, 2) + "." + 
						StringUtil.fomatInt(d.getDate(), 2) + fieldSeparator;
				}   
				if (includeTime)
				{
					date += StringUtil.fomatInt(d.getHours(), 2) + ":" +
						StringUtil.fomatInt(d.getMinutes(), 2) + ":" +
						StringUtil.fomatInt(d.getSeconds(), 2) + "." +
						StringUtil.fomatInt(d.getMilliseconds(), 3) + fieldSeparator;
				}
			}
			
			var level:String = "";
			if (includeLevel)
			{
				level = "[" + LogEvent.getLevelString(event.level) +
					"]" + fieldSeparator;
			}
			
			var category:String = includeCategory ?ILogger(event.target).category + fieldSeparator : "";
			
			var message:String = level + date + category + event.message;
			
			trace (message);
			if (_ui == null) return;
			var color:uint;
			switch (event.level)
			{
				case LogEventLevel.FATAL:
					color = 0xff0000;
					break;
				case LogEventLevel.ERROR:
					color = 0xdc143c;
					break;
				case LogEventLevel.WARN:
					color = 0xd2691e;
					break;
				case LogEventLevel.INFO:
					color = 0x228b22;
					break;
				case LogEventLevel.DEBUG:
					color = 0x4169e1;
					break;
				default:
					color = 0x000000;
			}
			_ui.append(decorate(level + event.message, color));
		}
//============================================================================
//PRIVATE
//============================================================================
		private function decorate(value:String, color:uint):String
		{
			return "<font color=\"#" + color.toString(16) + "\">" + value + "</font>";
		}
//============================================================================
//GETTER/SETTER
//============================================================================
		public function get ui():ILogUI
		{
			return _ui;
		}

		public function set ui(value:ILogUI):void
		{
			_ui = value;
		}

	}
}